package com.dmzapp.api.servlet;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dmzapp.api.db.AppDao;
import com.dmzapp.api.service.RedisAndDBService;
import com.dmzapp.api.util.ExchangeService;
import com.dmzapp.api.vo.OutRecord;

public class UserOutcomeHandler extends DMZSerlvet{
    Logger logger = LoggerFactory.getLogger("dmzlog");
    /**
     * 
     */
    private static final long serialVersionUID = -2080088995689402052L;
    private ObjectMapper mapper = new ObjectMapper();
    
    @Override
    protected void handle(HttpServletRequest request, HttpServletResponse response) {
        
        String type = request.getParameter("out_type");
        String amount = request.getParameter("amount");
        String username = (String) request.getAttribute("u");
        String deviceid = request.getParameter("d");
        String macAddress = request.getParameter("m");
        String ip = (String) request.getAttribute("i");
        String target = request.getParameter("target");
        Map<String, Object> responseMap = new HashMap<String, Object>();
        //设置最大值
        if (Float.valueOf(amount) > 30) {
            responseMap.put("status", "500");
            responseMap.put("errormsg", "金额过大");
            try {
                mapper.writeValue(response.getWriter(), responseMap);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return;
        }
        
        OutRecord record = new OutRecord();
        record.setAmount(amount);
        record.setType(type);
        record.setDeviceid(deviceid);
        record.setMac(macAddress);
        record.setIp(ip);
        record.setTarget(target);
        record.setUsername(username);
        
        boolean flg = false;
        
        String rest = RedisAndDBService.getUserRest(username);
        
        if (Double.valueOf(rest) < Double.valueOf(amount)) {
            responseMap.put("status", "500");
            responseMap.put("errormsg", "金额不足");
            try {
                mapper.writeValue(response.getWriter(), responseMap);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return ;
        } 
        if (ExchangeService.doExchange(record)) {
            if(AppDao.getInstance().insertUseroutrecord(record)){
                RedisAndDBService.reduceUserRest(username, amount);
                flg = true;
            }
        } else {
            responseMap.put("status", "500");
            responseMap.put("errormsg", "充值失败");
            try {
                mapper.writeValue(response.getWriter(), responseMap);
            } catch (Exception e) {
                logger.error("", e);
                e.printStackTrace();
            }
            
        }
        
        if (flg) {
            responseMap.put("status", "200");
            responseMap.put("amount", RedisAndDBService.getUserAmount(username));
            responseMap.put("rest", RedisAndDBService.getUserRest(username));
            responseMap.put("server_time", Calendar.getInstance().getTimeInMillis());
            try {
                mapper.writeValue(response.getWriter(), responseMap);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    }
}
